const axios = require('axios');
const cheerio = require('cheerio');
const { subDays, parse } = require('date-fns');
const cloud = require('wx-server-sdk');
cloud.init({
    env: 'lvtengweishi-4gvg0d2h59537d91',
    throwOnCode: false
});

// 初始化数据库
const db = cloud.database();
const newsCollection = db.collection('news');

// 配置信息
const WEBSITES = {
    "山东省农业农村厅_通知公告": {
        baseUrl: "http://nync.shandong.gov.cn/zwgk/tzgg/tfwj/",
        listSelector: "ul.news-list > span.newstxt",
        titleSelector: "a",
        dateSelector: "span.date",
        pageUrl: page => page > 0? `index_${page}.html` : "index.html",
        totalPages: 1
    }
};

// 请求头配置
const headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
};

// 日期有效性检查函数
function isWithinMonth(dateStr) {
    try {
        console.log("解析日期:", dateStr); // 添加日志
        const newsDate = parse(dateStr, 'yyyy-MM-dd', new Date());
        const oneMonthAgo = subDays(new Date(), 30);
        return newsDate >= oneMonthAgo;
    } catch (error) {
        console.error("日期解析失败:", dateStr, error.message); // 添加日志
        return false;
    }
}

// 主爬取函数
async function crawl() {
    const allResults = [];

    for (const [siteName, config] of Object.entries(WEBSITES)) {
        console.log(`开始爬取 ${siteName}...`);
        let outOfTimeCount = 0;

        for (let page = 0; page < config.totalPages; page++) {
            try {
                const pageUrl = new URL(config.pageUrl(page), config.baseUrl).href;
                console.log(`正在请求页面 URL: ${pageUrl}`);

                const response = await axios.get(pageUrl, { headers });
                const $ = cheerio.load(response.data);
                console.log(`成功加载页面数据: ${pageUrl}`);

                $(config.listSelector).each((i, element) => {
                    const title = $(config.titleSelector, element).text().trim();
                    // 调整日期选择器，直接从父元素的下一个兄弟元素中提取日期
                    const dateElement = $(element).next('span.date');
                    const dateStr = dateElement.length > 0? dateElement.text().trim() : "无日期";
                    const link = new URL($(config.titleSelector, element).attr('href'), config.baseUrl).href;

                    if (dateStr === "无日期") {
                        console.warn("未提取到日期，HTML结构:", dateElement.html());
                    }

                    if (isWithinMonth(dateStr) && /(葡萄|价格|优良品种)/.test(title)) {
                        allResults.push({
                            title,
                            date: dateStr,
                            link,
                            summary: "",
                            section: siteName,
                            createTime: db.serverDate()
                        });
                        console.log(`加入结果: ${JSON.stringify(allResults[allResults.length - 1])}`);
                        outOfTimeCount = 0;
                    } else {
                        console.log(`过滤内容 - 标题: ${title}, 日期: ${dateStr}`);
                        if (++outOfTimeCount >= 5) {
                            console.log(`${siteName} 连续5条超时，停止爬取`);
                            return allResults; // 提前返回已收集的数据
                        }
                    }
                });

                console.log(`${siteName} 第${page + 1}页完成`);
                await new Promise(resolve => setTimeout(resolve, 1000));

            } catch (error) {
                console.error(`爬取错误 (页面 ${page + 1}):`, error.message);
            }
        }
    }
    return allResults;
}

// 数据存储函数
async function saveToDatabase(results) {
    try {
        console.log('开始存储数据到数据库...');
        const batchSize = 100; // 每批插入100条
        const total = results.length;
        const tasks = [];

        for (let i = 0; i < total; i += batchSize) {
            const batch = results.slice(i, i + batchSize);
            tasks.push(newsCollection.add({
                data: batch
            }));
        }

        await Promise.all(tasks);
        console.log('数据存储完成! 共存储', total, '条记录');
        return {
            success: true,
            count: total
        };
    } catch (error) {
        console.error("存储数据库时发生错误:", error.message);
        return {
            success: false,
            error: error.message
        };
    }
}

// 云函数入口
exports.main = async (event, context) => {
    try {
        console.log('云函数触发，开始爬取...');
        const results = await crawl();
        console.log('爬取完成，结果数量:', results.length);

        if (results.length === 0) {
            console.warn("未爬取到任何结果!");
            return {
                code: 1,
                message: "未爬取到任何结果",
                data: null
            };
        }

        // 存储到数据库
        const saveResult = await saveToDatabase(results);
        if (saveResult.success) {
            return {
                code: 0,
                message: 'success',
                data: {
                    count: saveResult.count
                }
            };
        } else {
            throw new Error(saveResult.error);
        }

    } catch (error) {
        console.error('全局错误:', error.message);
        return {
            code: -1,
            message: error.message,
            data: null
        };
    }
};